package com.feishi.core.spring.websocket.handler;

import org.springframework.stereotype.Service;
import org.springframework.web.socket.*;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * @version 1.0
 * @auth caixq
 * @time 18-5-8 10:35
 **/
@Service
public class BaseTextWebSocketHandler extends TextWebSocketHandler {

    protected Set<WebSocketSession> socketSessions = new HashSet<>();


    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        super.afterConnectionEstablished(session);
        socketSessions.add(session);
    }

    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        super.handleMessage(session, message);
    }

    @Override
    /**
     * 接受客户端消息
     * */
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        super.handleTextMessage(session, message);
    }

    @Override
    protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
        super.handlePongMessage(session, message);
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        super.handleTransportError(session, exception);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        super.afterConnectionClosed(session, status);
        socketSessions.remove(session);
    }


    /**
     * 广播信息
     *
     * @param message
     * @return
     */
    public boolean broadcast(TextMessage message) {
        boolean allSendSuccess = true;
        for (WebSocketSession session : socketSessions) {
            try {
                if (session.isOpen()) {
                    session.sendMessage(message);
                }
            } catch (IOException e) {
                allSendSuccess = false;
            }
        }
        return allSendSuccess;
    }

    /**
     * 广播信息
     *
     * @param message
     * @return
     */
    public boolean broadcast(String message) {
        boolean allSendSuccess = true;
        for (WebSocketSession session : socketSessions) {
            try {
                if (session.isOpen()) {
                    session.sendMessage(new TextMessage(message));
                }
            } catch (IOException e) {
                allSendSuccess = false;
            }
        }
        return allSendSuccess;
    }



    /**
     * 广播信息
     *
     * @param message
     * @return
     */
    public boolean broadcast(byte[] message) {
        boolean allSendSuccess = true;
        for (WebSocketSession session : socketSessions) {
            try {
                if (session.isOpen()) {
                    session.sendMessage(new BinaryMessage(message));
                }
            } catch (IOException e) {
                allSendSuccess = false;
            }
        }
        return allSendSuccess;
    }

}
